<?php
require("config.php");
require("fonctions.php");
session_start() ;
if(!isset($_SESSION["login"]) || $_SESSION["login"] == "")
{   
  header("Location: index.php?error=4") ;
}
$action = $_GET["action"];

if($action=="login"){
    $login = $_POST["login"];
    $password = $_POST["password"];

    $sql="select * from users where login='$login' and password='$password'";
	if(mysql_num_rows($res = mysql_query($sql))){
		$row = mysql_fetch_object($res);
		
		// d&eacute;but de la sessions
	       session_start() ;
	    // enregistrement d'une variable de session, ici le login de l'utilisateur
	       	$_SESSION["login"] = $login ;
    		$_SESSION["profil"] = $row->profil ;
    		$_SESSION["codesap"] = $row->codesap ;
    		$_SESSION["id_user"] = $row->id ;
    		$codesap= $row->codesap ;
    		histo_connection($row->id,$row->profil);
	       if($row->profil=="RT"){
	       		
	       		header("Location:rt_acc.php?cur=10"); 
	       
	       }elseif($row->profil=="DIS"){

	       		header("Location:dis_acc1.php?codesap=$codesap&cur=18"); 
	       	
	       }elseif($row->profil=="MAN"){
	       	
	       		header("Location:man_acc.php?cur=11"); 
	       	
	       }elseif($row->profil=="LVDC"){
	       	
	       		header("Location:lv_acc.php?cur=1"); 
	       	
	       }elseif($row->profil=="RQ"){
	       	
	       		header("Location:rq_acc.php?cur=17"); 
	       	
	       }
	       
		
	}else{
		header("Location:index.php?error=3");
	}
	
		
}
if($action=="new_user"){
	$nom = $_POST["nom"];
	$prenom = $_POST["prenom"];
	$login = $_POST["login"];
	$password = $_POST["password"];
	$profil = $_POST["profil"];
	if(ifExistLogin($login)==FALSE){
		$sql="insert into users values('','$login','$password','$profil','$prenom','$nom',NOW(),'')";
		mysql_query($sql) or die ("PB: insertion $sql");
		header("Location:lv_ges_user.php?error=1&cur=2");
	}else{
		header("Location:lv_ges_user.php?error=1&cur=2");
	}
}

if($action=="mod_user"){
	$nom = $_POST["nom"];
	$prenom = $_POST["prenom"];
	$login = $_POST["login"];
	$password = $_POST["password"];
	$profil = $_POST["profil"];
	$id_user = $_POST["id_user"];
		
	$sql="update users set nom='$nom', prenom='$prenom',login='$login',password='$password',profil='$profil',crea_date=NOW() where id=$id_user ";
	mysql_query($sql) or die ("PB: insertion $sql");
	header("Location:lv_ges_user.php?error=1&cur=2");
}

if($action=="sup_user"){
	$id_user=$_GET["id_user"];
	$sql="delete from users where id=$id_user";
	mysql_query($sql);
	header("Location:lv_ges_user.php?error=7&cur=2");
}

if($action=="load_csv"){
	$repertoireDestination = "data/";
	//echo'<br>';
	$nomDestination        = "fichier_du_".date("Y_m_d").".csv";
	
	if (is_uploaded_file($_FILES["monfichier"]["tmp_name"])) {
	    if (rename($_FILES["monfichier"]["tmp_name"],
	                   "$repertoireDestination/$nomDestination")) {
			$sql="insert into FICHIER_CSV(nom,date) values('$nomDestination',NOW())";
			mysql_query($sql);
	    } else {
	       /* echo "Le d&eacute;placement du fichier temporaire a &eacute;chou&eacute;".
	                " v&eacute;rifiez l'existence du r&eacute;pertoire ".$repertoireDestination;*/
	    }          
	} else {
	  //  echo "Le fichier n a pas &eacute;t&eacute; upload&eacute;(trop gros ?)";
}


	
	// local
 $sql="LOAD DATA LOCAL INFILE '$repertoireDestination/$nomDestination' REPLACE INTO TABLE data_temp FIELDS TERMINATED BY ';' ENCLOSED BY '\"' ESCAPED BY '|' LINES TERMINATED BY '\r\n' IGNORE 1 LINES";

	$req = mysql_query($sql) or die (mysql_error()); 
	
	
// traitement des nouveaux distributeurs 
	
	$sql2="select distinct code_sap from data_temp
			where code_sap not in (select codesap from distributeurs)";
	$res2 = mysql_query($sql2);
	
	while($r2 = mysql_fetch_object($res2)){
		$sql3="select code_sap,distributeur,groupe from data_temp where code_sap='$r2->code_sap'";
		$res3 = mysql_query($sql3);
		$r3 = mysql_fetch_object($res3);
		$description = str_replace("'", " ", $r3->distributeur);
		$id_dis  =  create_distributeur($r3->code_sap,$description,$r3->groupe,$val,$val);
			
	}
	$sql4 = "select distinct vague from data_temp";
	$res4= mysql_query($sql4);
	while($r4 = mysql_fetch_object($res4)){
		createVague($r4->vague);
	}
	
	//echo"***************************************** LOAD effectu&eacute; avec succ&eacute;s ! ****************************************";
	
	header("Location:lv_acc.php?cur=1&error=5");
	
}

if($action=="load_csv2"){
	$repertoireDestination = "data/";
	//echo'<br>';
	$repertoireDestination ="data/";
	$nomDestination        = "fichier_du_".date("Y_m_d").".csv";
	
	if (is_uploaded_file($_FILES["monfichier"]["tmp_name"])) {
	    if (rename($_FILES["monfichier"]["tmp_name"],
	                   $repertoireDestination.$nomDestination)) {
			$sql="insert into FICHIER_CSV(nom,date) values('$nomDestination',NOW())";
			mysql_query($sql);
	    } else {
	       /* echo "Le d&eacute;placement du fichier temporaire a &eacute;chou&eacute;".
	                " v&eacute;rifiez l'existence du r&eacute;pertoire ".$repertoireDestination;*/
	    }          
	} else {
	   // echo "Le fichier n'a pas &eacute;t&eacute; upload&eacute; (trop gros ?)";
}

	
	// local
 $sql='LOAD DATA LOCAL INFILE \'D:/wamp/www/webasert/data/'.$nomDestination.'\' ' .
 		'REPLACE INTO TABLE `distrib_temp` FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' ESCAPED BY \'|\' ' .
 		'LINES TERMINATED BY \'\\r\\n\' (codesap,distri,groupe,manager,zone,reseau,nom,email,fonction,login,password)';
 		
	$req = mysql_query($sql) or die (mysql_error()); 
	
	
	//echo"*****************************************LOAD effectu&eacute; avec succ&eacute;s ! ****************************************";
	//header("Location:lv_acc.php?cur=1&error=5");
	
}




if($action=="run_dis"){
		$sql="select * from distrib_temp";
		$res = mysql_query($sql);
			
		while($r =  mysql_fetch_object($res)){
			
			// creation distributeur
			//$id_dis  =  create_distributeur($r->codesap,$r->distri,$r->groupe,getIdZone($r->zone),getIdReseau($r->reseau));
			$id_dis = getIdDisbyCodeSap($r->codesap);
			
			 $val = ifEmailExist($r->email);
			if($val=="nok"){
			// creation manager
			$id_man = create_manager($r->manager,$r->email,$r->fonction);
			}else{
				$id_man = getIdManagerByEmail($r->email);
			}
			//asso distributeurs managers
			create_AssoDisMan($id_dis,$id_man,getIdZone($r->zone),getIdReseau($r->reseau));
						
		}
		
		//echo ".............................. Ex&eacute;cution termin&eacute;es !!!!!!!!!!!!!!!!!!!!!!!!!";
}

if($action == "run_login"){
	$sql="select login,password,manager from distrib_temp group by email";
	$res = mysql_query($sql);
	while($r = mysql_fetch_object($res)){
		
		$t = explode(" ",$r->manager);
		$nom = $t[0];
		$prenom = $t[1];
		
		 $i = "insert into users values('','$r->login','$r->password','MAN','$nom','$prenom',NOW(),'' )";
		//echo"<br>";
		mysql_query($i);
	}
	//echo ".............................. Ex&eacute;cution termin&eacute;es !!!!!!!!!!!!!!!!!!!!!!!!!";
}

if($action == "load_csv3"){
	$repertoireDestination = "data/";
	//echo'<br>';
	$repertoireDestination ="data/";
	$nomDestination        = "fichier_du_".date("Y_m_d").".csv";
	
	if (is_uploaded_file($_FILES["monfichier"]["tmp_name"])) {
	    if (rename($_FILES["monfichier"]["tmp_name"],
	                   $repertoireDestination.$nomDestination)) {
			$sql="insert into FICHIER_CSV(nom,date) values('$nomDestination',NOW())";
			mysql_query($sql);
	    } else {
	       /* echo "Le d&eacute;placement du fichier temporaire a &eacute;chou&eacute;".
	                " v&eacute;rifiez l'existence du r&eacute;pertoire ".$repertoireDestination;*/
	    }          
	} else {
	   // echo "Le fichier n'a pas &eacute;t&eacute; upload&eacute; (trop gros ?)";
}

	
	// local
 $sql='LOAD DATA LOCAL INFILE \'D:/wamp/www/webasert/data/'.$nomDestination.'\' ' .
 		'REPLACE INTO TABLE `contact_temp` FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' ESCAPED BY \'|\' ' .
 		'LINES TERMINATED BY \'\\r\\n\' (codesap,login,mdp,profil,email)';
 		
	$req = mysql_query($sql) or die (mysql_error()); 
	
	
	//echo"*****************************************LOAD effectu&eacute; avec succ&eacute;s ! ****************************************";
	}

	
	if($action=="run_contact"){
		$sql="select * from contact_temp";
		$res = mysql_query($sql);
		
		while($r  = mysql_fetch_object($res)){
			$id_distrib = getIdDisbyCodeSap($r->codesap);
			 $i= "insert into contacts values('','','','$r->email',$id_distrib,'DIS')"; //echo"<br>";
			mysql_query($i);
			if(ifExistLogin($r->codesap)==FALSE){
				 $u="insert into users (login,password,profil,codesap) values('$r->login','$r->mdp','DIS','$r->codesap')"; //echo"<br>";
				mysql_query($u);
			}
		}
		
		
		//echo"*****************************************creation effectu&eacute; avec succ&eacute;s ! ****************************************";
	}
	
	
if($action=="crea_push"){
	$descrip = $_POST["descrip"];
	$datedeb = $_POST["datedeb"];
	$message = $_POST["message"];
	$datefin = $_POST["datefin"];
	
	 $sql="insert into vagues_camp values('','$descrip','$datedeb','$datefin',NOW(),'NON_DEMARRER','$message')";
	mysql_query($sql);
	header("Location:lv_plan.php?cur=4&error=6");
}


if($action=="modif_push"){
	$descrip = $_POST["descrip"];
	$datedeb = $_POST["datedeb"];
	$message = $_POST["message"];
	$datefin = $_POST["datefin"];
   $sql="update vagues_camp set descrip='$descrip' , date_deb='$datedeb',date_fin='$datefin',crea_date=NOW(),statut='NON_DEMARRER',message='$message'";
	mysql_query($sql);
	header("Location:lv_plan.php?cur=4&error=6");
}

if($action=="sup_camp"){
	
	$id=$_GET["id"];
	$sql="delete from vagues_camp where id=$id";
	mysql_query($sql);
	header("Location:lv_plan.php?cur=4&error=7");
}

if($action=="sup_contact"){
	$codesap = $_GET["codesap"];
	$id=$_GET["id"];
	$sql="delete from contacts where id=$id";
	mysql_query($sql);
	header("Location:lv_details.php?cur=3&error=7&codesap=$codesap");
}

if($action=="sup_dis"){
	
	$id=$_GET["id"];
	$sql="delete from distributeurs where id=$id";
	mysql_query($sql);
	header("Location:lv_ges_dis.php?cur=3&error=7");
}

if($action=="run_camp"){
	$id_push = $_POST["id_push"];
	$sql="update vagues_camp set statut='EN_COURS' where id=$id_push";
	mysql_query($sql);
	
	$sql1="select message from vagues_camp where statut='EN_COURS'";
	if(mysql_num_rows($res1 = mysql_query($sql))){
		$r1 = mysql_fetch_object($res1);
		$message_mail=$r1->message;
	}
	
	$m="(select m.email from distributeurs d
		join asso_dis_man on id_distrib = d.id
		join managers m on id_man = m.id order by d.codesap)
		
		union all
		
		(select r.email from distributeurs d
		join asso_dis_resp_qual on id_distrib = d.id
		join respon_qual r on id_rq = r.id order by d.codesap)
		
		union all
		
		(select c.email from distributeurs d
		join contacts c on c.id_distrib=d.id order by d.codesap
		)";
	$resm = mysql_query($sql);
	
	while($rm = mysql_fetch_object($resm)){
		
		$subject="Rappel Identification";
		$message="Bonjour votre identifiant est : $r3->login , votre mot de passe : $r3->password <br> $message_mail";
		$headers="";
		$headers = 'From: admin@lvdc.com' . "\r\n" .
	     'Reply-To: admin@lvdc.com' . "\r\n" .
	     'X-Mailer: PHP/' . phpversion();
		//echo $message;
		 @@mail($rm->email, $subject, $message, $headers);
	}
	
	header("Location:lv_plan.php?cur=4&error=8");
}

if($action=="modif_dis"){
	$id_distrib = $_POST["id_distrib"];
	$id_man = $_POST["id_man"];
	$nom = $_POST["nom"];
	$prenom = $_POST["prenom"];
	$email = $_POST["email"];
	$fonction = $_POST["fonction"];
	$codesap = $_POST["codesap"];
	
	$nom_contact = $_POST["nom_contact"];
	$prenom_contact = $_POST["prenom_contact"];
	$email_contact = $_POST["email_contact"];
	$fonction_contact = $_POST["fonction_contact"];
	
	$rq_nom = $_POST["rq_nom"];
	$rq_prenom = $_POST["rq_prenom"];
	$rq_email = $_POST["rq_email"];
	$id_rq = $_POST["id_rq"];
	
	$val_contact = $_POST["val_contact"];
	
	if($id_man>0){
	 $sql="update managers set nom='$nom',prenom='$prenom',email='$email', fonction='$fonction' where id='$id_man'";
		mysql_query($sql);
	}else{
		$sql="insert into managers values('','$nom','$prenom','$email','Manager')";
		mysql_query($sql);
		$id = mysql_insert_id();
		create_AssoDisMan($id_distrib,$id,$id_zone,$id_res);
	}
	
	$sqlq="update respon_qual set nom='$rq_nom',prenom='$rq_prenom',email='$rq_email' where id='$id_rq'";
	mysql_query($sqlq);
	
	if($val_contact=="add_contact"){
		 $sql1="insert into contacts values('','$nom_contact','$prenom_contact','$email_contact','$id_distrib','$fonction_contact')";
		mysql_query($sql1);
	}
	
	if($val_contact=="mod_contact"){
		$id_contact = $_POST["id_contact"];
		
		$sql2="update contacts set nom='$nom_contact',prenom='$prenom_contact',email='$email_contact',fonction='$fonction_contact' where id='$id_contact'";
		mysql_query($sql2);
	}
	
	$profil = $_SESSION["profil"];
	if($profil=="LVDC"){
		header("Location:lv_details.php?cur=3&error=6&codesap=$codesap");
	}elseif($profil=="MAN"){
		header("Location:man_details.php?cur=13&error=6&codesap=$codesap");
	}elseif($profil=="RQ"){
		header("Location:rq_details.php?cur=3&error=6&codesap=$codesap");
	}elseif($profil=="RT"){
		header("Location:lv_details.php?cur=3&error=6&codesap=$codesap");
	}
}

if($action=="modif_dis2"){
	$id_distrib = $_POST["id_distrib"];
	$id_man = $_POST["id_man"];
	$id_rq = $_POST["id_rq"];
	
	$nom = $_POST["nom"];
	$prenom = $_POST["prenom"];
	$email = $_POST["email"];
	$fonction = $_POST["fonction"];
	$codesap = $_POST["codesap"];
	
	$nom_contact = $_POST["nom_contact"];
	$prenom_contact = $_POST["prenom_contact"];
	$email_contact = $_POST["email_contact"];
	$fonction_contact = $_POST["fonction_contact"];
	
	$qnom = $_POST["qnom"];
	$qprenom = $_POST["qprenom"];
	$qemail = $_POST["qemail"];
	$qfonction = $_POST["qfonction"];
	
	$val_contact = $_POST["val_contact"];
	
	/* $sql="update managers set nom='$nom',prenom='$prenom',email='$email', fonction='$fonction' where id='$id_man'";
	mysql_query($sql);
	
	 $sqlq="update respon_qual set nom='$qnom',prenom='$qprenom',email='$qemail', fonction='$qfonction' where id='$id_rq'";
	mysql_query($sqlq);*/
	
	if($val_contact=="add_contact"){
		 $sql1="insert into contacts values('','$nom_contact','$prenom_contact','$email_contact','$id_distrib','$fonction_contact')";
		mysql_query($sql1);
	}
	
	if($val_contact=="mod_contact"){
		$id_contact = $_POST["id_contact"];
		
		$sql2="update contacts set nom='$nom_contact',prenom='$prenom_contact',email='$email_contact',fonction='$fonction_contact' where id='$id_contact'";
		mysql_query($sql2);
	}
	header("Location:dis_acc.php?cur=3&error=6&codesap=$codesap");
}

if($action=="mod_dis_new"){
	$distributeur = $_POST["distributeur"];
	$nom_manager = $_POST["nom_manager"];
	$email = $_POST["email"];
	$codesap = $_POST["codesap"];
	$id_dis = $_GET["id_dis"];
	$zone = $_POST["zone"];
	$reseau = $_POST["reseau"];
	$groupe = $_POST["groupe"];
	$profil = $_POST["profil"];
	
	$sql="update distributeurs set nom='$distributeur', groupe='$groupe', id_zone='$zone',id_reseau='$reseau' where id=$id_dis";
	mysql_query($sql);
	
	$id_man = getIdManagerByDis($id_dis);
	$t = explode(" ",$nom_manager);
	$nom = $t[0];
	$prenom = $t[1];
	
	$sql2="update managers set nom='$nom',prenom='$prenom' where id=$id_man";
	mysql_query($sql2);
	if($profil=="RT"){
		header("Location:rt_list_dis.php?cur=16&error=6");
	}else{
		header("Location:dis_contacts.php?cur=3&action=mod_dis_new&id_dis=$id_dis&cur=3&error=6");
	}
}

if($action=="run_test"){
	$sql="select codesap  from distrib_temp";
	$res = mysql_query($sql);
	
	while($r  = mysql_fetch_object($res)){
		
		 $s = "update distributeurs set login=$r->codesap where codesap='$r->codesap'";
		mysql_query($s);
	}
		//echo ".............................. Ex&eacute;cution termin&eacute;es !!!!!!!!!!!!!!!!!!!!!!!!!";
}


if($action=="modif_arg"){
	$codesap = $_POST["codesap"];
	$sql="select * from data_temp where code_sap=$codesap order by id_interv";
	$res = mysql_query($sql);
	
		while($r  = mysql_fetch_object($res)){
			$arg_res_old = $_POST['arg_res_'.$r->id_interv.$r->id_lvdc];
			
			// rechercher dans la chaine ";" et remplacer par vide
			$espace = array("\n", "\r", "\s", "\t", ";", "'");
			$arg_res2 = str_replace($espace," ",$arg_res_old);
			$arg_res = str_replace(CHR(13).CHR(10),"",$arg_res2);
			
			$demande_re_satifact = $_POST['demande_re_satifact_'.$r->id_interv];
			$lvdc = $_POST['id_lvdc_'.$r->id_interv.$r->id_lvdc];
			$sql2 = "update data_temp set arg_res='$arg_res',demande_re_satifact='$demande_re_satifact' where id_interv=$r->id_interv and  id_lvdc=$lvdc";
			mysql_query($sql2);
			getStatutInterv($r->id_interv);
			//echo"<br>";
		}
		header("Location:dis_interv.php?cur=6&error=6&codesap=$codesap");
}

if($action=="modif_arg_rt"){
	$groupe = $_POST["groupe"];
	$code_sap = $_POST["code_sap"];
	
	$nom_dis=$_POST["nom_dis"];
	$code_sap=$_POST["code_sap"];
	$statut=$_POST["statut"];
	$nps_rt=$_POST["nps_rt"];
	$q11=$_POST["q11"];
	$groupe1=$_POST["groupe1"];
	$id_interv=$_POST["id_interv"];
	if($groupe=="all"){
		 $sql="select * from data_temp where  arg_res<>'' order by id_interv desc";
	}else{
		//$sql="select * from data_temp where groupe like '%$groupe%' order by id_interv";
		
				$sql = "select * from data_temp where 1=1 and vague in(select descrip from vagues_camp where statut='EN_COURS') ";
		$where = "";
if($id_interv>0){
			$colon="id_interv";
           	$where.=" and $colon='$id_interv'";
		}
if($nom_dis>0){
			$colon="code_sap";
           	$where.=" and $colon='$nom_dis'";
		}
if($code_sap>0){
			$colon="code_sap";
           	$where.=" and $colon='$code_sap'";
		}
if(!empty($statut)){
			$colon="statut";		
			$val = substr($statut,0,14);        	
			$where.=" and $colon like '%$val%'";
		}
if($nps_rt!="0" && $nps_rt!=""){
			$colon="nps_rt";
           	$where.=" and $colon='$nps_rt'";
		}
if($q11!="0" && $q11!=""){
			$colon="q11";
           	$where.=" and $colon='$q11'";
		}
if($groupe1!="0" && $groupe1!=""){
			$colon="groupe";
           	$where.=" and $colon='$groupe1'";
		}				
		$sql.=$where;
		
	}
	//echo $sql;echo"<br>";
	$res = mysql_query($sql);
	//print_r($_POST);
		while($r  = mysql_fetch_object($res)){
			
			//*********** traitement de l'arumentaire r�seau
			$arg_res_old = $_POST['arg_res_'.$r->id_interv.$r->id_lvdc];	
			
			// rechercher dans la chaine ";" et remplacer par vide
			$espace = array("\n", "\r", "\s", "\t", ";", "'");
			$arg_res2 = str_replace($espace," ",$arg_res_old);
			//echo"<br>";
			$arg_res = str_replace(CHR(13).CHR(10)," ",$arg_res2); 
			
			//************ traitement de l'argumentaire RT
			$arg_rt_old = $_POST['arg_rt_'.$r->id_interv.$r->id_lvdc];	
			
			// rechercher dans la chaine ";" et remplacer par vide
			$espace = array("\n", "\r", "\s", "\t", ";", "'");
			$arg_rt2 = str_replace($espace," ",$arg_rt_old);
			
			$arg_rt = str_replace(CHR(13).CHR(10)," ",$arg_rt2);
			//$arg_rt = str_replace(chr(13),"",$arg_rt2);
			
			$demande_re_satifact = $_POST['demande_re_satifact_'.$r->id_interv];	
			$lvdc = $_POST['id_lvdc_'.$r->id_interv.$r->id_lvdc];
			  $sql2 = "update data_temp set arg_res='$arg_res', arg_rt='$arg_rt', demande_re_satifact='$demande_re_satifact' where id_interv=$r->id_interv"; 
		//	echo"<br>";
			  mysql_query($sql2);
		
			  $decision = $_POST['decision_'.$r->id_interv];
			  
			    // *******************traitement de l'envoie du email 
			  $old_decision = getOldDecision($r->id_interv);
			  if($old_decision!=$decision){
				  	$subject="Infos : D�cision RT ";
					$message="Bonjour, merci d'aller dans votre espace pour consulter votre d�cision RT";
					$headers="";
					$headers = 'From: admin@lvdc.com' . "\r\n" .
				     'Reply-To: admin@lvdc.com' . "\r\n" .
				     'X-Mailer: PHP/';
					$id_distrib = getIdDisbyCodeSap($r->code_sap);
					
					$c="select email from contacts where id_distrib=$id_distrib";
					$resc =  mysql_query($c);
					while($rc = mysql_fetch_object($resc)){
						// @@mail($rc->email, $subject, $message, $headers);
					}
			  }
			  
			 $sql3 = "update data_temp set decision='$decision' where id_interv=$r->id_interv";
			  mysql_query($sql3);
			
			
			
			getStatutInterv($r->id_interv);
			//echo"<br>";
			$arg_rt=" ";
			$id_lvdc=" ";
		}
		if($groupe=="all"){
			header("Location:rt_interv.php?cur=8&error=6&retour=ok&groupe=$groupe");
		}else{
			header("Location:rt_interv.php?cur=8&error=6&retour=ok");
		}
		
}

if($action=="add_man"){
	$nom = $_POST["nom"];
	$prenom = $_POST["prenom"];
	$email = $_POST["email"];
	$login = $_POST["login"];
	$mdp = $_POST["mdp"];
	$adresse = $_POST["adresse"];
	
	$sql="insert into managers(nom,prenom,email,fonction) values('$nom','$prenom','$email','MANAGER')";
	mysql_query($sql);
	$sql2="insert into users(login,password,profil,nom,prenom) values('$login','$mdp','MAN','$nom','$prenom')";
	mysql_query($sql2);
	header("Location:rt_list_dis.php?cur=16&error=6");
}


if($action=="add_dis"){
	$nom = $_POST["nom"];
	$prenom = $_POST["prenom"];
	$groupe = $_POST["groupe"];
	$zone = $_POST["zone"];
	$reseau = $_POST["reseau"];
	$codesap = $_POST["codesap"];
	$distributeur = $_POST["distributeur"];
	$nom_manager = $_POST["nom_manager"];
	$id_manager = $_POST["id_manager"];
	$login = $_POST["login"];
	$mdp = $_POST["mdp"];
	$id_dis  =  create_distributeur($codesap,$distributeur,$groupe,$zone,$reseau);
	
	//$id_man = create_manager($nom_manager,$email,$fonction);
			
	//asso distributeurs managers
	create_AssoDisMan($id_dis,$id_manager,$zone,$reseau);
	
	$sql2="insert into users(login,password,profil,nom,prenom,codesap) values('$login','$mdp','DIS','$nom','$prenom','$codesap')";
	mysql_query($sql2);
	
	$profil = $_GET["profil"];
	if($profil=="LVDC"){
		header("Location:lv_ges_dis.php?cur=3&error=6");
	}elseif($profil=="MAN"){
		header("Location:man_list_dis.php?cur=3&error=6");
	}elseif($profil=="RQ"){
		header("Location:rq_list_dis.php?cur=3&error=6");
	}elseif($profil=="RT"){
		header("Location:rt_list_dis.php?cur=16&error=6");
	}
}

if($action=="add_decision_mail"){
	 
	$message1=$_POST["message"];
	$sujet=$_POST["sujet"];
	$codesap = $_POST["codesap"];
	$id_interv = $_POST["id_interv"];
	
	$d ="delete from rt_synth_mail where id_interv=$id_interv";
	if(mysql_query($d)){
		
	}
	 $sql="insert into rt_synth_mail values ('','$sujet','$message1',NOW(),'$codesap','$id_interv')";
	mysql_query($sql);
	
	 $subject = $sujet;
     $message = $message1;
     $headers = 'From: admin@lvdc.com' . "\r\n" .
     'Reply-To: admin@lvdc.com' . "\r\n" .
     'X-Mailer: PHP/' . phpversion();
     	
$sql2="(select m.email from distributeurs d
			join asso_dis_man on id_distrib = d.id
			join managers m on id_man = m.id where d.codesap=4205 order by d.codesap)
			
			union 
			
			(select r.email from distributeurs d
			join asso_dis_resp_qual on id_distrib = d.id
			join respon_qual r on id_rq = r.id where d.codesap=4205 order by d.codesap)
			
			union 
			
			(select c.email from distributeurs d
			join contacts c on c.id_distrib=d.id where d.codesap=4205 order by d.codesap
			)";
		$res2=mysql_query($sql2);
		while($r2 = mysql_fetch_object($res2)){
			 @@mail($r2->email, $subject, $message, $headers);
		}
	header("Location:rt_synth.php?cur=9&error=6");
}

if($action=="aff_user"){
	$id_user = $_POST["id_user"];
	$profil = $_POST["profil"];
	$groupe = $_POST["groupe"];
	$sql_man ="delete from asso_dis_man where id_man=$id_user";
	mysql_query($sql_man);
	
	
	$distrib=$_POST["distrib"];
	if($profil=="MAN"){
		foreach ($distrib as $choix){
			  $sql_i="insert into asso_dis_man values ('$choix','$id_user','','')"; //echo"<br>";
			mysql_query($sql_i);
		}
	}
	if($profil=="RQ"){
	foreach ($distrib as $choix){
			 $sql_i="insert into asso_dis_resp_qual values ('$choix','$id_user')";
			mysql_query($sql_i);
		}
	}
	if($profil=="DIS"){
		foreach ($distrib as $choix){
			 $codesap = getCodesap($choix);
			$up = "update users set codesap='$codesap' where id=$id_user";
			mysql_query($up);
		}
	}
	header("Location:lv_affected_user.php?groupe=$groupe&cur=2&id_user=$id_user&error=9&profil=$profil");
}


if($action=="aff_man"){
	
	$id_manager = $_POST["manager"];
	$id_dis1 = $_POST["nom_dis1"];
	$id_dis2 = $_POST["nom_dis2"];
	$id_dis3 = $_POST["nom_dis3"];
	$id_dis4 = $_POST["nom_dis4"];
	$id_dis5 = $_POST["nom_dis5"];
	$id_dis6 = $_POST["nom_dis6"];
	$id_dis7 = $_POST["nom_dis7"];
	$id_dis8 = $_POST["nom_dis8"];
	$id_dis9 = $_POST["nom_dis9"];
	$id_dis10 = $_POST["nom_dis10"];
	
	//asso distributeurs managers
	if($id_dis1>0){
		create_AssoDisMan($id_dis1,$id_manager,$zone,$reseau);
	}
	if($id_dis2>0){
		create_AssoDisMan($id_dis2,$id_manager,$zone,$reseau);
	}
	if($id_dis3>0){
		create_AssoDisMan($id_dis3,$id_manager,$zone,$reseau);
	}
	if($id_dis4>0){
		create_AssoDisMan($id_dis4,$id_manager,$zone,$reseau);
	}
if($id_dis5>0){
		create_AssoDisMan($id_dis5,$id_manager,$zone,$reseau);
	}
if($id_dis6>0){
		create_AssoDisMan($id_dis6,$id_manager,$zone,$reseau);
	}
if($id_dis7>0){
		create_AssoDisMan($id_dis7,$id_manager,$zone,$reseau);
	}
if($id_dis8>0){
		create_AssoDisMan($id_dis8,$id_manager,$zone,$reseau);
	}
if($id_dis9>0){
		create_AssoDisMan($id_dis9,$id_manager,$zone,$reseau);
	}
if($id_dis10>0){
		create_AssoDisMan($id_dis10,$id_manager,$zone,$reseau);
	}
	
	header("Location:rt_list_dis.php?cur=16&error=6");
	
}


if($action=="oublie"){
	$fonction=$_POST["fonction"];
	$email =$_POST["email"];
	if($fonction=="DIS"){
		$sql="select id,nom,prenom from contacts where email='$email'";
		if(mysql_num_rows($res = mysql_query($sql))>0){
			$r1 = mysql_fetch_object($res);
			$nom = $r1->nom;
			$prenom = $r1->prenom;
		}
	}
	if($fonction=="MAN"){
		$sql2="select id,nom,prenom from managers where email='$email'";
		if(mysql_num_rows($res2 = mysql_query($sql2))>0){
			$r2 = mysql_fetch_object($res2);
			$id = $r2->id;
			$nom = $r2->nom;
			$prenom = $r2->prenom;
		}
	}
	
	 $sql3="select * from users where nom='$nom' and prenom='$prenom' and profil='$fonction'";
	if(mysql_num_rows($res3 = mysql_query($sql3))>0){
		
		$r3 = mysql_fetch_object($res3);
		
		$subject="Rappel Identification";
		$message="Bonjour votre identifiant est : $r3->login , votre mot de passe : $r3->password";
		$headers="";
		$headers = 'From: admin@lvdc.com' . "\r\n" .
	     'Reply-To: admin@lvdc.com' . "\r\n" .
	     'X-Mailer: PHP/' . phpversion();
		//echo $message;
		 @@mail($email, $subject, $message, $headers);
		header("Location:index.php?cur=16&error=10");
	}else{
		header("Location:index.php?cur=16&error=11");
	}
	
}

if($action=="modif_vag"){
	$vague = $_POST["vague"];
	$id =$_POST["id"];
	$sql="update vagues set descrip='$vague' where id='$id'";
	mysql_query($sql);
	header("Location:lv_vagues.php?cur=20&error=6");
}

if($action=="sup_vag"){

	$id= $_GET["id"];
	$val= $_GET["val"];
	
	$sql1="delete from vagues where id='$id'";
	mysql_query($sql1);
	
	$sql2="delete from data_temp where vague like '%$val%'";
	mysql_query($sql2);
	
	header("Location:lv_vagues.php?cur=20&error=6");
	
}

?>